//
//  HICEmoticonToolbar.swift
//  vibo
//
//  Created by jht-seven on 16/7/4.
//  Copyright © 2016年 jht-seven. All rights reserved.
//

import UIKit

//buttontag中间值
private let tagNum = 1000

// 表情键盘类型设计
enum HICEmoticonToolbarType: Int{
    // 默认
    case Default = 0
    // Emoji表情
    case Emoji = 1
    //浪小花
    case Lxh = 2
}

class HICEmoticonToolbar: UIStackView {
    
    var currentButton: UIButton?
    
    // 声明一个闭包告知外界哪个按钮点击
    var toolbarClosure:((type: HICEmoticonToolbarType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 供外界调用的方法
    func setCurrnetButtons(tag: Int){
        //通过tag获取button
        let button = viewWithTag(tag + tagNum) as! UIButton
        
        //如果当前的button等于我们选中的button直接返回
        if currentButton == button {
            return
        }
        
        button.selected = true
        currentButton?.selected = false
        currentButton = button
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        //排列方式
        axis = .Horizontal
        //乃填充方式
        distribution = .FillEqually
        backgroundColor = RandomColor()
        
        let defaultButton = addChildButtons("left", title: "默认", type: .Default)
        
        let emojiButton = addChildButtons("mid", title: "Emoji", type: .Emoji)
        
        let lxhButton = addChildButtons("right", title: "浪小花", type: .Lxh)
        
        //首次默认按钮wie选中状态
        defaultButton.selected = true
        currentButton = defaultButton
    }
    
    // MARK: - 监听方法
    func buttonClick(button: UIButton){
        //如果当前的button等于我们选中的button直接返回
        if currentButton == button {
            return
        }
        
        button.selected = true
        currentButton?.selected = false
        currentButton = button
        
        //执行闭包
        toolbarClosure?(type: HICEmoticonToolbarType(rawValue: button.tag - tagNum)!)
    }
    
    //创建button的公共方法
    func addChildButtons(imgName: String, title: String, type: HICEmoticonToolbarType) -> UIButton{
        let button = UIButton()
        
        //设置tag
        button.tag = type.rawValue + tagNum
        
        //添加点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_selected"), forState: UIControlState.Selected)
        
        button.setTitle(title, forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        self.addArrangedSubview(button)
        
        return button
    }
    
    
    
}
